<?php
/**
 * Created by PhpStorm.
 * User: 热心观众
 * Date: 2018/9/27
 * Time: 14:06
 */

namespace app\admin\controller;


use think\Db;

class Problem
{
//    问题列表
    public function ProblemList()
    {
        $plist = Db::table('zz_problem')->order('zz_problem_id','desc')->select();
//        dump( $plist);

        foreach ($plist as $v){

            $cate[] = Db::table('zz_cate')
                    ->where('zz_cate_id',$v['zz_problem_cid'])
                    ->select();
        }
//        dump($cate);
        $res = [];
        for ($i=0;$i < sizeof($cate);$i++){
            for ($j=0;$j < sizeof($cate[$i]);$j++){
                $res[$i] = $cate[$i][$j];
            }
        }
//        dump($res);die;

        for ($i=0;$i<sizeof($plist);$i++){
            for($j=0;$j<sizeof($res);$j++){
                if ($res[$j]['zz_cate_id'] == $plist[$i]['zz_problem_cid']){
                    $plist[$i]['zz_problem_cate'] = $res[$j]['zz_cate_name'];
                }
            }
        }
//        dump($plist);


        foreach($plist as $v){

            $user[] = Db::table('zz_user')
                    ->where('zz_id',$v['zz_uid'])
                    ->select();

        }
//        dump($user);

        $u = [];
        for ($i=0;$i < sizeof($user);$i++){
            for ($j=0;$j < sizeof($user[$i]);$j++){
                $u[$i] = $user[$i][$j];
            }
        }
//        dump($u);

        for ($i=0;$i<sizeof($plist);$i++){
            for ($j=0;$j < sizeof($u);$j++){
                if ($plist[$i]['zz_uid'] == $u[$j]['zz_id']){
                    $plist[$i]['zz_uname'] = $u[$j]['zz_name'];
                }
            }
        }

//        dump($plist);


        $total = count(Db::table('zz_problem')->field('zz_problem_id')->select());

        return view('problem/problem-list',['plist'=>$plist,
            'total'=>$total,
        ]);
    }
    
    // 问题详情
    public function ProblemDetails($id)
    {
        $problem = Db::table('zz_problem')
                 ->where('zz_problem_id',$id)
                 ->find();
        $user = Db::table('zz_user')
            ->field('zz_name')
            ->where('zz_id',$problem['zz_uid'])
            ->find();
        $comment =  Db::table('zz_comment')
            ->where('zz_problem_id',$problem['zz_problem_id'])
            ->select();

        foreach ($comment as $v){
            $u[] = Db::table('zz_user')
                ->where('zz_id',$v['zz_comment_uid'])
                ->find();
        }

        for ($i=0; $i < sizeof($comment); $i++) {
            for ($j=0; $j < sizeof($u); $j++) {
                if (!empty($u[$i])) {
                    if ($u[$j]['zz_id'] == $comment[$i]['zz_comment_uid']) {
                        $comment[$i]['zz_admin_name'] = $u[$j]['zz_name'];
                    }
                }
            }
        }

        return view('problem/problem-details',['problem'=>$problem,
            'user'=>$user,
            'comment'=>$comment
        ]);
    }

//    删除问题

    public function ProblemDel()
    {
        $id = input('id');
        Db::table('zz_problem')->where('zz_problem_id',$id)->delete();
        Db::table('zz_comment')->where('zz_problem_id',$id)->delete();

        $res = [];
        $res['status'] = 1;
        return json($res);
    }



}